/*
    时间转换
*/
export const format = (value: any, type: string) => {
    // var stringTime = '2012-10-12 22:37:33';
    let date: any = new Date(Date.parse(value))
    console.log(date)
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    month = month > 9 ? month : `0${ month }`
    let day = date.getDate()
    day = day > 9 ? day : `0${ day }`
    let hour = date.getHours()
    let min = date.getMinutes()
    let sec = date.getSeconds()
    hour = hour > 9 ? hour : `0${ hour }`
    min = min > 9 ? min : `0${ min }`
    sec = sec > 9 ? sec : `0${ sec }`
    if(type == '-') return `${ year }-${ month }-${ day }` 
    return `${ year }-${ month }-${ day } ${ hour }:${ min }:${ sec }`
}

/**
 * 
 * 处理后端传过来的时间
 */
export const handleTime = (value: string) => {
    let reg =/(\d{4})\-(\d{2})\-(\d{2})/
    let date = value.replace(reg,"$1年$2月$3日")
    return date
}

/*
    清空 reactive 里的值 
*/
export const clearObject = (value: any) => {
    const keys = Object.keys(value)
    let obj: { [name: string]: string } = {}
    keys.forEach((item) => {
        obj[item] = ''
    })
    return obj
}

/**
 * 处理 el-cascader 需要的信息
 */
export const handleElCascaderOptions = (value: any, addTypeLabelData: any) => {
    let typeLabelList: any = []
    value.forEach((item: any) => {
        let obj: any = {}
        obj['value'] = item.tid 
        obj['label'] = item.t_name  
        obj['children'] = []
        item.label.forEach((lItem: any) => {
            let obj1: any = {}
            obj1['value'] = lItem.lid 
            obj1['label'] = lItem.l_name 
            if(addTypeLabelData.typeLabelListById) {
                addTypeLabelData.typeLabelListById.label.map((target: any) => { 
                    if(target.lid == lItem.lid) {
                        obj1['disabled'] = true 
                    }
                })
            }
            obj['children'].push(obj1)
        })
        typeLabelList.push(obj)
    })
    return typeLabelList
}

/**
 * 
 * 处理添加或更改诗歌标签时传递给后端的标签信息
 */
export const handleTypeLidID = (selectLabel: any, deleteLabelArr: any) => {
    let addLidString: string = ''
    selectLabel.forEach((item: any, index: any) => {
        index == selectLabel.length - 1 ? addLidString += `${ item[1] }` : addLidString += `${ item[1] },`
    })
    let deleteLidString = ''
    if(deleteLabelArr.length != 0) {
        deleteLidString = deleteLabelArr.join(',')
    }
    return {
        addLidString: addLidString,
        deleteLidString: deleteLidString
    }
}

/**
 * 
 * 处理图片路径
 */
export const getAssetsFile = (url: any) => {
    // console.log('import.meta.url', import.meta.url)
    return new URL(`../assets/${url}`, import.meta.url).href
}

/**
 * 
 * 搜索关键字高亮
 */

export const highlightText = (text: string, keyword: string) => {
    if (!keyword) {
        return text
    }
    const regex = new RegExp(keyword, 'gi')
    let startIndex = 0
    let endIndex = 0
    text.search(regex) - 6 < 0 ? startIndex = text.search(regex) : startIndex = text.search(regex) - 6
    return text.slice(startIndex, text.search(regex) + 12).replace(regex, match => `<span class="highlight">${match}</span>`);
}